package com.hqully.smartfumehood.common.utils;

import com.hqully.smartfumehood.entity.AirData;
import com.hqully.smartfumehood.entity.FumeHoodData;
import com.hqully.smartfumehood.entity.SmokeData;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * <p>
 * 解码硬件设备消息
 * <p>
 *
 * @author liulingyu
 * @date 2022/6/24 9:09
 * @Version 1.0
 */
@Slf4j
public class DecodeUtil {
    /**
     * <p>
     *     解码硬件发送的数据
     * </p>
     * @date 2022/6/24 20:42 <br>
     * @author liulingyu <br>
     * @param data 硬件发送的数据
     * @return {@link HashMap< String, Object>} 解码后的对象
     */
    public static HashMap<String,Object> decodeData(String data){

        if (data.contains("z") && data.contains("q")){

            return decodeFumeHoodData(data);

        }else if (data.contains("a") && data.contains("s")){
            return decodeAirAndSmokeData(data);
        }
       return new HashMap<String,Object>();

    }

    /**
     * <p>
     *     解码通风柜数据
     * </p>
     * @date 2022/6/24 20:43 <br>
     * @author liulingyu <br>
     * @param data 硬件设备发送的数据
     * @return {@link HashMap< String, Object>} 通风柜数据对象map
     */
    public static HashMap<String, Object> decodeFumeHoodData(String data){

        String raw = data;

        data = data.substring(0,data.indexOf("q")+1);

        data = data.replaceAll("[a-z\\s+]", "");
        FumeHoodData fumeHoodData = new FumeHoodData();

        try {
            Scanner sc = new Scanner(data);

            sc.useDelimiter("/");


            long APId = sc.nextLong();
            if (!VerifyUtil.inInterval(APId,1000000000L,9999999999L)){
                throw new IllegalArgumentException();
            }
            fumeHoodData.setAPId(APId);
            fumeHoodData.setVavId(sc.nextLong());
            fumeHoodData.setSpeedS(sc.nextFloat());
            fumeHoodData.setDis(sc.nextFloat());
            fumeHoodData.setAngle(sc.nextInt());
            fumeHoodData.setOpenCtrl(sc.nextInt());
            fumeHoodData.setMode(sc.nextInt());
            fumeHoodData.setOpenLight(sc.nextInt());

            fumeHoodData.setTime(LocalDateTime.now());
        } catch (Exception e) {
            log.warn("decode fail , raw data is {}",raw);
            return null;
        }



       log.info("fumeHoodData is {}",fumeHoodData);

        HashMap<String, Object> resultMap = new HashMap<>(1);

        resultMap.put("fumeHoodData",fumeHoodData);
        return resultMap;
    }


    /**
     * <p>
     *     解码空气传感器和烟雾传感器的数据
     * </p>
     * @date 2022/6/24 20:44 <br>
     * @author liulingyu <br>
     * @param data 硬件设备发送的数据
     * @return {@link HashMap< String, Object>} 包含空气数据对象和烟雾数据对象列表
     */
    public static  HashMap<String, Object> decodeAirAndSmokeData(String data){


        LocalDateTime time = LocalDateTime.now();

        String raw = data;

        data = data.substring(0,data.indexOf("s")+1);

        data = data.replaceAll("[a-z\\s+]", "");


        AirData airData = new AirData();
        List<SmokeData> smokeDataList = new ArrayList<SmokeData>();

        try {
            Scanner sc = new Scanner(data);

            sc.useDelimiter("/");

            Long APId = sc.nextLong();
            if (!VerifyUtil.inInterval(APId,1000000000L,9999999999L)){
                throw new IllegalArgumentException();
            }
            Long airId = sc.nextLong();
            airData.setAPId(APId);
            airData.setAirId(airId);
            airData.setTemp(sc.nextFloat());
            airData.setHumi(sc.nextFloat());
            airData.setTvoc(sc.nextFloat());
            airData.setHcho(sc.nextFloat());
            airData.setCo2(sc.nextFloat());


            airData.setTime(time);

            log.info("airData is {}",airData);

            for (int i = 0; i < 4; i++) {
                SmokeData smokeData = new SmokeData();
                smokeData.setAPId(APId);
                smokeData.setAirId(airId);
                smokeData.setSmokeId((long) i);
                smokeData.setSmokeConc(sc.nextInt());
                smokeData.setTime(time);
                smokeDataList.add(smokeData);
            }

            smokeDataList.forEach((smokeData) ->{
                smokeData.setSmokeAlarm(sc.nextInt());
                log.info("{}",smokeData);
            });
        } catch (Exception e) {
            log.warn("decode fail , raw data is {}",raw);
            return null;
        }
        HashMap<String, Object> resultMap = new HashMap<>(2);

        resultMap.put("airData",  airData);
        resultMap.put("smokeDataList", smokeDataList);

        return resultMap;
    }
}
